/**
 * @project lauwsys
 */
package com.chimoz.lauwsys.repository.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import com.chimoz.lauwsys.domain.Brand;
import com.chimoz.lauwsys.repository.BrandRepository;

/**
 * @author Christian Teguh
 * @created 4 Okt 2011 15.02.14
 */
@Repository
public class JBrandRepository implements BrandRepository {

	@Value("${jdbc.sql.brand.insert}")
	private String insertBrandQuery;
	
	@Value("${jdbc.sql.brand.update}")
	private String updateBrandQuery;
	
	@Value("${jdbc.sql.brand.remove}")
	private String removeBrandQuery;
	
	@Value("${jdbc.sql.brand.queryById}")
	private String brandByIdQuery;
	
	@Value("${jdbc.sql.brand.queryAll}")
	private String allBrandQuery;
	
	@Autowired
	private SimpleJdbcTemplate simpleJdbcTemplate;
	
	
	public Brand saveBrand(final Brand brand) {
		int count = 0; Long id;
		if ( brand.getId() != null ) {
			count = simpleJdbcTemplate.update(updateBrandQuery, brand.getName(), brand.getDesc(), brand.getId());
			id = brand.getId();
		} else {
			JdbcTemplate jdbcTemplate = (JdbcTemplate) simpleJdbcTemplate.getJdbcOperations();
			KeyHolder keyHolder = new GeneratedKeyHolder();
			count = jdbcTemplate.update( new PreparedStatementCreator() {
																			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
																				PreparedStatement ps = con.prepareStatement(insertBrandQuery, new String[] { "brand_id" });
																				ps.setString(1, brand.getName());
																				ps.setString(2, brand.getDesc());
																				return ps;
																			}
																		 },
										 keyHolder								 
										);
			id = keyHolder.getKey().longValue();
		}
		if ( count > 0 ) {
			return getBrandById(id);
		} else {
			return null;
		}
		
	}

	public void removeBrand(Brand brand) {
		simpleJdbcTemplate.update(removeBrandQuery, brand.getId());

	}

	public Brand getBrandById(Long id) {
		Brand brand = new Brand(id);
		return simpleJdbcTemplate.queryForObject(brandByIdQuery, brandRowMapper, id);
	}

	public List<Brand> listBrand(Map<String, String> criteria) {
		String limit = criteria.get("limit"); 
		String offset = criteria.get("offset");
		return simpleJdbcTemplate.query(allBrandQuery + " LIMIT " + limit + " OFFSET " + offset, brandRowMapper);
	}
	
	private RowMapper<Brand> brandRowMapper = new RowMapper<Brand>() {
		public Brand mapRow(ResultSet rs, int rowNum) throws SQLException {
			Brand brand = new Brand();
			brand.setId(rs.getLong("brand_id"));
			brand.setName(rs.getString("brand_name"));
			brand.setDesc(rs.getString("brand_desc"));
			brand.setCtime(rs.getInt("brand_ctime"));
			brand.setMtime(rs.getInt("brand_mtime"));
			return brand;
		}
	};

}
